<?php
namespace app\admin\controller;

use think\Controller;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
class Email extends Base{
    public function index(){

    }

    public function sendmail(){
        $mail = new PHPMailer(true);                              // Passing `true` enables exceptions
        try {
            //Server settings
            $mail->SMTPDebug = 2;                                 // Enable verbose debug output
            $mail->isSMTP();                                      // Set mailer to use SMTP
            $mail->Host = 'smtp.163.com';  // Specify main and backup SMTP servers
            $mail->SMTPAuth = true;                               // Enable SMTP authentication
            $mail->Username = 'anineok@163.com';                 // SMTP username
            $mail->Password = 'www.37212918.com';                           // SMTP password
            $mail->SMTPSecure = 'ssl';                            // Enable TLS encryption, `ssl` also accepted
            $mail->Port = 994;                                    // TCP port to connect to

            //Recipients
            $mail->setFrom('@example.com', 'Mailer');
            $mail->addAddress('joe@example.net', 'Joe User');     // Add a recipient
            $mail->addAddress('ellen@example.com');               // Name is optional
            $mail->addReplyTo('info@example.com', 'Information');
            $mail->addCC('cc@example.com');
            $mail->addBCC('bcc@example.com');

            //Attachments
            // $mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
            // $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name

            //Content
            $mail->isHTML(true);                                  // Set email format to HTML
            $mail->Subject = 'Here is the subject';
            $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
            $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

            $mail->send();
            echo 'Message has been sent';
        } catch (Exception $e) {
            echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
        }
    }

    public function postmail(){
 
        $mail = new PHPMailer();  
        $mail->isSMTP();// 使用SMTP服务  
        $mail->CharSet = "utf8";// 编码格式为utf8，不设置编码的话，中文会出现乱码  
        $mail->Host = "smtp.163.com";// 发送方的SMTP服务器地址  
        $mail->SMTPAuth = true;// 是否使用身份验证  
        $mail->Username = "xxxx@163.com";// 发送方的163邮箱用户名  
        $mail->Password = "xxxx";// 发送方的邮箱密码，注意用163邮箱这里填写的是“客户端授权密码”而不是邮箱的登录密码！  
        $mail->SMTPSecure = "ssl";// 使用ssl协议方式  
        $mail->Port = 994;// 163邮箱的ssl协议方式端口号是465/994  
        $mail->From= "xxxx";  //这个可以不用
        $mail->Helo= "xxxx";  //这个可以不用
        $mail->setFrom("xxxx@163.com","xxxx");// 设置发件人信息，如邮件格式说明中的发件人，这里会显示为Mailer(xxxx@163.com），Mailer是当做名字显示  
        $mail->addAddress($_POST['mail'],'Liang');// 设置收件人信息，如邮件格式说明中的收件人，这里会显示为Liang(yyyy@163.com)  
        $mail->IsHTML(true);  
        $mail->Subject = $_POST['sub'];// 邮件标题  
        $mail->Body = $_POST['con'];// 邮件正文  
        if(!$mail->send()){// 发送邮件  
            echo "Message could not be sent.";  
            echo "Mailer Error: ".$mail->ErrorInfo;// 输出错误信息  
        }else{  
            echo 'Message has been sent.';  
        }  
    }
}
